package com.lcxw.hxzbapi.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcxw.hxzbapi.dao.mapper.RoomRecommendMapper;
import com.lcxw.hxzbapi.entity.pojo.RoomRecommend;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.pojo.YwpdRoom;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.entity.response.ResponseData;
import com.lcxw.hxzbapi.entity.response.RoomRecommendResp;
import com.lcxw.hxzbapi.service.RoomRecommendService;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import com.lcxw.hxzbcommon.common.StringUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class RoomRecommendServerImpl extends
    ServiceImpl<RoomRecommendMapper, RoomRecommend> implements
    RoomRecommendService {

    @Resource
    YwpdRoomService ywpdRoomService;

    @Resource
    YwpdMemberService ywpdMemberService;

    //添加一个房间推荐位
    @Override
    public Response addRoomRecommend(long userId, int position) {
        YwpdRoom ywpdRoom = ywpdRoomService.selectRoomByUserId(userId);
        if (ywpdRoom == null) {
            return Response.error(ErrorCode.INVALID_PARAM, "该用户还没有直播间");
        }
        if (selectByRoomId(ywpdRoom.getRoomId()) != null) {
            return Response.error(ErrorCode.ALREADY_EXIST, "改用户的房间已经设置过推荐位");
        }
        RoomRecommend roomRecommend = new RoomRecommend();
        roomRecommend.setRoomId(ywpdRoom.getRoomId());
        roomRecommend.setPosition(position);
        roomRecommend.setAddTime(StringUtil.getDateTime());
        save(roomRecommend);
        return Response.ok();
    }

    //获取推荐位列表
    @Override
    public Response getList(Integer position, int pageNum, int pageSize) {

        IPage<RoomRecommend> page = new Page<>(pageNum, pageSize);
        QueryWrapper<RoomRecommend> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(position != null, RoomRecommend::getPosition, position);
        wrapper.lambda().orderByAsc(RoomRecommend::getPosition);
        page = page(page, wrapper);
        long total = page.getTotal();
        List<RoomRecommend> records = page.getRecords();
        List<RoomRecommendResp> list = new ArrayList<>();
        for (RoomRecommend roomRecommend : records
        ) {
            Long roomId = roomRecommend.getRoomId();
            YwpdRoom ywpdRoom = ywpdRoomService.selectRoomByRoomId(roomId);
            long userId = ywpdRoom.getUserId();
            YwpdMember ywpdMember = ywpdMemberService.selectYwpdMemberByUserId(userId);
            RoomRecommendResp roomRecommendResp = new RoomRecommendResp();
            BeanUtils.copyProperties(ywpdMember, roomRecommendResp);
            BeanUtils.copyProperties(ywpdRoom, roomRecommendResp);
            BeanUtils.copyProperties(roomRecommend, roomRecommendResp);
            list.add(roomRecommendResp);
        }
        ResponseData data = new ResponseData();
        data.setTotal(total);
        data.setList(list);
        return Response.ok(data);

    }

    //修改房间推荐位
    @Override
    public Response modRoomRecommend(int id, int position) {
        UpdateWrapper<RoomRecommend> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(RoomRecommend::getId, id);
        updateWrapper.lambda().set(RoomRecommend::getPosition, position);
        update(updateWrapper);
        return Response.ok();
    }

    //获取房间的推荐位位置，0为没有推荐位
    @Override
    public int getRoomRecommendPosition(long roomId) {

        int position = 0;

        RoomRecommend roomRecommend = selectByRoomId(roomId);
        if (roomRecommend != null) {
            position = roomRecommend.getPosition();
        }

        return position;
    }

    //通过roomId来获取房间推荐位
    public RoomRecommend selectByRoomId(long roomId) {
        QueryWrapper<RoomRecommend> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RoomRecommend::getRoomId, roomId);
        return getOne(wrapper);
    }
}
